package com.sfh.cp.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sfh.cp.R;
import com.sfh.cp.util.UtilFace;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:表情控件
 *
 * @date 2017/7/12
 */
public class WidgetFaceLayout extends RecyclerView implements BaseQuickAdapter.OnItemClickListener {


    /***
     * 点击回调
     */
    public interface OnItemSelectListener {

        /**
         * 选择
         */
        void onClickSelect(String name,int res);

    }

    protected OnItemSelectListener listener;


    public WidgetFaceLayout(@NonNull Context context) {

        super(context);
        init();
    }

    public WidgetFaceLayout(@NonNull Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);
        init();
    }

    public WidgetFaceLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {

        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        FaceAdapter pullToRefreshAdapter = new FaceAdapter();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 7);
        this.setAdapter(pullToRefreshAdapter);
        this.setLayoutManager(gridLayoutManager);
        pullToRefreshAdapter.setOnItemClickListener(this);
    }

    /***
     *
     * @param listener
     */
    public void addOnItemSelectListener(OnItemSelectListener listener) {

        this.listener = listener;

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        if (listener == null) {
            return;
        }
        Integer res = (Integer) adapter.getItem(position);
        // 添加图片
        listener.onClickSelect(UtilFace.facesName.get(position), res);

    }


    class FaceAdapter extends BaseQuickAdapter<Integer, BaseViewHolder> {

        public FaceAdapter() {

            super(R.layout.widget_face_item,UtilFace.facesRes);

        }

        @Override
        protected void convert(BaseViewHolder helper, Integer item) {

            ImageView iv = helper.getView(R.id.ivItem);

            Picasso.with(getContext())
                    .load(item).config(Bitmap.Config.RGB_565)
                    .into(iv);

        }
    }



}
